package com.example.testplanetorbit

import android.graphics.*

/**
 * 椭圆轨道上的小圆点或者标记点的父类
 */
abstract class OrbitData {

    class OvalConfig(
        var horizontal_r: Int = 190,  //水平半径
        var horizontal_r_min: Int  = 170,  //水平半径最小值
        var horizontal_r_max: Int  = 190,   //水平搬家最大值

        var vertical_r: Int  = 90,   //垂直半径
        var vertical_r_max: Int  = 120,   //垂直半径最大值
        var vertical_r_min: Int  = 90,   //垂直半径最小值

        var degress: Float = -45f, //画布旋转角度

        var orbitColor: String = "#FFFFFF",

        var tags : ArrayList<OvalTagDot.Data> = arrayListOf(),

        var circle: Int = 1) {

        var centerX: Float = 0f
        var centerY: Float = 0f
        var mParentHeight : Int = 0
        var mParentWidth : Int = 0

        fun changeVertical(touchRadio: Float) {
            vertical_r = ((vertical_r_max - vertical_r) * touchRadio + vertical_r_min).toInt()
        }

        fun changeHorizontal(touchRadio: Float) {
            horizontal_r = ((horizontal_r_max - horizontal_r_min) * touchRadio + horizontal_r_min).toInt()
        }
    }

    companion object {
        const val OVAL_R1 = 6.0f
        const val OVAL_R1_MIN = 3.0f

        const val OVAL_TAG_R1 = 24.0f
        const val OVAL_TAG_R1_MIN = 16.0f

        const val ALPHA_MAX = 255
        const val ALPHA_MIN = 60

        const val INVALIDATE_TIMES1 = 3000 //控制速度，时间间隔
    }

    var orbitOvalPaint: Paint? = null
    var re21 : RectF? = null

    var mOvalCofig : OvalConfig? = null

    var resetPath = false

    var perspectiveDiff: Float = 0f

    private val degressRadio = 1.0f


    open fun init(ovalConfig: OvalConfig) {
        this.mOvalCofig = ovalConfig

        if (orbitOvalPaint == null) {
            orbitOvalPaint = Paint()
            orbitOvalPaint?.isAntiAlias = true
            orbitOvalPaint?.color = Color.parseColor(ovalConfig.orbitColor)
            orbitOvalPaint?.style = Paint.Style.STROKE
            orbitOvalPaint?.strokeWidth = 0.8f
        }
    }

    fun getDegress() = mOvalCofig?.degress

    fun changeDegress(up:Boolean) {
        if (mOvalCofig?.degress == null) {
            return
        }
        if (up) {
            if (mOvalCofig!!.degress < OrbitsView.TOUCH_DEGRESS_MAX) {
                mOvalCofig!!.degress += degressRadio
            } else {
                mOvalCofig!!.degress = OrbitsView.TOUCH_DEGRESS_MAX
            }
        } else {
            if (mOvalCofig!!.degress > OrbitsView.TOUCH_DEGRESS_MIN) {
                mOvalCofig!!.degress -= degressRadio
            } else {
                mOvalCofig!!.degress = OrbitsView.TOUCH_DEGRESS_MIN
            }
        }
    }

    fun changeVerticalR(touchRadio: Float) {
        mOvalCofig?.changeVertical(touchRadio)
        resetPath = true
    }

    fun resetPath() {
        resetPath = false
    }

    fun setParentSize(width: Int, height: Int) {
        val changed =  (mOvalCofig?.mParentWidth != width || mOvalCofig?.mParentHeight != height) //宽高改变了
        mOvalCofig?.mParentWidth = width
        mOvalCofig?.mParentHeight = height
        if ((mOvalCofig?.centerX?:0f) <= 0 ||
            (mOvalCofig?.centerY?:0f) <= 0 ||
            changed) {
            mOvalCofig?.centerX = width * 1.0f / 2
            mOvalCofig?.centerY = height * 1.0f / 2
            optimazeConfig()
        }
    }

    fun drawRectF(canvas: Canvas?) {
        val left = (mOvalCofig?.centerX?:0f) - (mOvalCofig?.horizontal_r?:0)
        val right = (mOvalCofig?.centerX?:0f) + (mOvalCofig?.horizontal_r?:0)
        val top = (mOvalCofig?.centerY?:0f) - (mOvalCofig?.vertical_r?:0) + perspectiveDiff * 2f
        val bottom = (mOvalCofig?.centerY?:0f) + (mOvalCofig?.vertical_r?:0) + perspectiveDiff * 2f

        re21 = RectF(left, top, right, bottom)
        orbitOvalPaint?.let { canvas?.drawOval(re21!!, it) }
    }

    abstract fun changeHorizontal(moveLeft:Boolean)

    abstract fun drawOval1(canvas: Canvas?)

    abstract fun setStepLock(lock:Boolean)

    abstract fun setPathMeasure1()

    abstract fun drawText(canvas: Canvas?)

    abstract fun optimazeConfig()

    abstract fun clickDot(point:PointF) : OvalTagDot.Data?
}